package com.chengyu.core.controller.trends;

import cn.hutool.core.collection.CollectionUtil;
import com.chengyu.core.component.OperationLog;
import com.chengyu.core.controller.UserBaseController;
import com.chengyu.core.domain.CommonConstant;
import com.chengyu.core.domain.enums.TrendsEnums;
import com.chengyu.core.domain.form.TrendsForm;
import com.chengyu.core.domain.result.WalkTrendsCommentResult;
import com.chengyu.core.domain.result.WalkTrendsResult;
import com.chengyu.core.entity.CommonPage;
import com.chengyu.core.entity.CommonResult;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.model.*;
import com.chengyu.core.service.walk.WalkTrendsDownloadNumService;
import com.chengyu.core.service.walk.WalkTrendsDownloadService;
import com.chengyu.core.service.walk.WalkTrendsService;
import com.chengyu.core.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "逛逛")
@Controller
public class TrendsController extends UserBaseController {

    @Autowired
    private WalkTrendsService walkTrendsService;
    @Autowired
    private WalkTrendsDownloadService walkTrendsDownloadService;
    @Autowired
    private WalkTrendsDownloadNumService walkTrendsDownloadNumService;


    @ApiOperation(value = "逛逛首页")
    @ResponseBody
    @RequestMapping(value = "/common/trends/search", method = RequestMethod.GET)
    public CommonResult<CommonPage<WalkTrendsResult>> search(Integer goodsId, Boolean isRefreshGetNumber, TrendsForm form, Integer page, Integer pageSize) {
        form.setStatus(CommonConstant.SUS_INT);
        form.setWalkMember(getCurrentWalkMember());
        CommonPage<WalkTrendsResult> list = walkTrendsService.getTrendsList(null, 1, false, goodsId, isRefreshGetNumber, form, page, pageSize);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "平台首页")
    @ResponseBody
    @RequestMapping(value = "/common/trends/searchPlatformHome", method = RequestMethod.GET)
    public CommonResult<CommonPage<WalkTrendsResult>> searchPlatformHome(Integer goodsId, Boolean isRefreshGetNumber, TrendsForm form, Integer page, Integer pageSize) {
        form.setStatus(CommonConstant.SUS_INT);
        form.setWalkMember(getCurrentWalkMember());
        CommonPage<WalkTrendsResult> list = walkTrendsService.getTrendsList(null, 2, false, goodsId, isRefreshGetNumber, form, page, pageSize);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "原创素材")
    @ResponseBody
    @RequestMapping(value = "/common/trends/searchOriginalMaterials", method = RequestMethod.GET)
    public CommonResult<CommonPage<WalkTrendsResult>> searchOriginalMaterials(Integer goodsId, Boolean isRefreshGetNumber, TrendsForm form, Integer page, Integer pageSize) {
        form.setStatus(CommonConstant.SUS_INT);
        form.setWalkMember(getCurrentWalkMember());
        CommonPage<WalkTrendsResult> list = walkTrendsService.getTrendsList(null, 3, false, goodsId, isRefreshGetNumber, form, page, pageSize);
        return CommonResult.success(list);
    }


    @ApiOperation(value = "逛逛首页-模拟生成单个ai")
    @ResponseBody
    @RequestMapping(value = "/common/trends/searchForAi", method = RequestMethod.GET)
    public CommonResult<List<WalkTrendsResult>> searchForAi(String showIds, Integer goodsId, Boolean isRefreshGetNumber, TrendsForm form, Integer page, Integer pageSize) {
        form.setStatus(CommonConstant.SUS_INT);
        form.setWalkMember(getCurrentWalkMember());
        List<WalkTrendsResult> list = walkTrendsService.getTrendsListForAi(1, false, showIds, goodsId, isRefreshGetNumber, form, page, pageSize);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "逛逛精选页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "trendsId", value = "首条动态ID"),
            @ApiImplicitParam(name = "walkMemberId", value = "创作者ID"),
            @ApiImplicitParam(name = "trendsType", value = "1文案类>>2视频类")
    })
    @ResponseBody
    @RequestMapping(value = "/common/trends/getSelectList", method = RequestMethod.GET)
    public CommonResult<CommonPage<WalkTrendsResult>> getSelectList(Integer location, Integer onlyOneTrendsId, Boolean isGg, Integer goodsId, Boolean isRefreshGetNumber, Integer trendsId, Integer walkMemberId, Integer trendsType, Integer page, Integer pageSize) {
        TrendsForm form = new TrendsForm();
        form.setFirstTrendsId(trendsId);
        form.setWalkMemberId(walkMemberId);
        form.setStatus(CommonConstant.SUS_INT);
        form.setOnlyOneTrendsId(onlyOneTrendsId);
        if (trendsType == 1) {
            form.setTypeList(CollectionUtil.newArrayList(TrendsEnums.TrendsType.GOODS.getValue(), TrendsEnums.TrendsType.DISCOVERY.getValue(), TrendsEnums.TrendsType.BUYERSHOW.getValue()));
        } else {
            form.setType(TrendsEnums.TrendsType.VIDEO.getValue());
        }
        form.setWalkMember(getCurrentWalkMember());
//        form.setIsShowInGg(isGg);
        CommonPage<WalkTrendsResult> list = walkTrendsService.getTrendsList(null, location, false, goodsId, isRefreshGetNumber, form, page, pageSize);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "动态详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "trendsId", value = "动态ID")
    })
    @ResponseBody
    @RequestMapping(value = "/common/trends/get", method = RequestMethod.GET)
    public CommonResult<WalkTrendsResult> get(Integer trendsId) {
        WalkTrendsResult result = walkTrendsService.getTrendsDetail(trendsId);
        return CommonResult.success(result);
    }

    @OperationLog
    @ApiOperation(value = "关注取关动态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "trendsId", value = "动态ID")
    })
    @ResponseBody
    @RequestMapping(value = {"/member/trends/collectOrNo"}, method = RequestMethod.POST)
    public CommonResult<String> collectOrNoGoods(Integer trendsId) {
        walkTrendsService.collectOrCancelTrends(getCurrentWalkMember(), trendsId);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "动态评论列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "trendsId", value = "动态ID")
    })
    @ResponseBody
    @RequestMapping(value = "/common/trends/getCommentList", method = RequestMethod.GET)
    public CommonResult<CommonPage<WalkTrendsCommentResult>> search(Integer trendsId, Integer page, Integer pageSize) {
        CommonPage<WalkTrendsCommentResult> list = walkTrendsService.getTrendsCommentList(trendsId, getCurrentWalkMember(), null, null, page, pageSize);
        return CommonResult.success(list);
    }

    @OperationLog
    @ApiOperation(value = "评论动态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "trendsId", value = "动态ID"),
            @ApiImplicitParam(name = "commentId", value = "针对某条评论ID"),
            @ApiImplicitParam(name = "content", value = "内容")
    })
    @ResponseBody
    @RequestMapping(value = {"/member/trends/comment"}, method = RequestMethod.POST)
    public CommonResult<WalkTrendsComment> comment(Integer trendsId, Integer commentId, String content) throws ServiceException {
        if (StringUtils.isBlank(content)) {
            throw new ServiceException("trends.comment.empty");
        }
        WalkTrendsComment comment = walkTrendsService.addComment(getCurrentWalkMember(), trendsId, commentId, content);
        return CommonResult.success(comment);
    }

    @OperationLog
    @ApiOperation(value = "点赞取消点赞评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId", value = "评论ID")
    })
    @ResponseBody
    @RequestMapping(value = {"/member/comment/likeOrNo"}, method = RequestMethod.POST)
    public CommonResult<String> collectOrNoComment(Integer commentId) {
        walkTrendsService.likeOrCancelComment(getCurrentWalkMember(), commentId);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "浏览动态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "trendsId", value = "动态ID"),
    })
    @ResponseBody
    @RequestMapping(value = {"/common/trends/addViewNums"}, method = RequestMethod.POST)
    public CommonResult<String> addViewNums(Integer trendsId) {
        walkTrendsService.addViewNums(trendsId, 1);
        return CommonResult.success(null);
    }

    @OperationLog
    @ApiOperation(value = "添加用户动态下载记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "walkTrendsDownload", value = "动态对象")
    })
    @ResponseBody
    @RequestMapping(value = {"/member/trends/addTrendsDownloadLog"}, method = RequestMethod.POST)
    public CommonResult<String> addTrendsDownloadLog(WalkTrendsDownload walkTrendsDownload) throws ServiceException {
        WalkMember walkMember = this.getCurrentWalkMember();
        walkTrendsDownloadService.addDownloadLog(walkTrendsDownload, walkMember);
        return CommonResult.success(null);
    }

    @OperationLog
    @ApiOperation(value = "查看用户动态下载记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "walkTrendsDownload", value = "动态对象")
    })
    @ResponseBody
    @RequestMapping(value = {"/member/trends/listTrendsDownloadLog"}, method = RequestMethod.GET)
    public CommonResult<CommonPage<WalkTrendsDownload>> listTrendsDownloadLog(Integer type, Integer page, Integer pageSize) throws ServiceException {
        WalkMember walkMember = this.getCurrentWalkMember();
        CommonPage<WalkTrendsDownload> walkTrendsDownloadCommonPage = walkTrendsDownloadService.listDownloadLog(null, null, type, null, walkMember.getId(), page, pageSize);
        return CommonResult.success(walkTrendsDownloadCommonPage);
    }

    @OperationLog
    @ApiOperation(value = "验证用户是否可下载")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "verifyTrendsDownload", value = "动态对象")
    })
    @ResponseBody
    @RequestMapping(value = {"/member/trends/verifyTrendsDownload"}, method = RequestMethod.GET)
    public CommonResult<WalkTrendsDownloadNum> verifyTrendsDownload(Integer trendsId, Integer id, Integer goodsId) throws ServiceException {
        return walkTrendsDownloadService.verifyTrendsDownload(trendsId, id, goodsId);
    }

    @OperationLog
    @ApiOperation(value = "验证用户是否可下载-是否已经下载")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "verifyTrendsDownload", value = "动态对象")
    })
    @ResponseBody
    @RequestMapping(value = {"/member/trends/verifyTrendsDownloadIsOther"}, method = RequestMethod.GET)
    public CommonResult<WalkTrendsDownloadNum> verifyTrendsDownloadIsOther(Integer trendsId) throws ServiceException {
        return walkTrendsDownloadService.verifyTrendsDownloadIsOther(trendsId);
    }

    @ResponseBody
    @RequestMapping(value = {"/member/trends/editVerifyTrendsDownloadNum"}, method = RequestMethod.POST)
    public CommonResult<WalkTrendsDownloadNum> editVerifyTrendsDownloadNum(WalkTrendsDownloadNum walkTrendsDownloadNum) throws ServiceException {
        walkTrendsDownloadNumService.editDownloadNum(walkTrendsDownloadNum);
        return CommonResult.success(null);
    }

    @ResponseBody
    @RequestMapping(value = {"/member/trends/addVerifyTrendsDownloadNum"}, method = RequestMethod.POST)
    public CommonResult<WalkTrendsDownloadNum> addVerifyTrendsDownloadNum(Integer trendsId, Integer type, WalkTrendsDownloadNum walkTrendsDownloadNum) throws ServiceException, ParseException {
        walkTrendsDownloadNumService.addDownloadNum(trendsId, type, walkTrendsDownloadNum, this.getCurrentWalkMember(), this.getCurrentMember());
        return CommonResult.success(null);
    }

    @ResponseBody
    @RequestMapping(value = {"/member/trends/delVerifyTrendsDownloadNum"}, method = RequestMethod.POST)
    public CommonResult<WalkTrendsDownloadNum> delVerifyTrendsDownloadNum(Integer id) throws ServiceException {
        walkTrendsDownloadNumService.delDownloadNum(id);
        return CommonResult.success(null);
    }

    @ResponseBody
    @RequestMapping(value = {"/member/trends/getVerifyTrendsDownloadNumList"}, method = RequestMethod.GET)
    public CommonResult<CommonPage<WalkTrendsDownloadNum>> getVerifyTrendsDownloadNumList(Integer status, Integer type,
                                                                                          @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                                          @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) throws ServiceException {

        CommonPage<WalkTrendsDownloadNum> walkTrendsDownloadNumCommonPage = walkTrendsDownloadNumService.listDownloadNum(null, null, null, null, status, type, this.getCurrentWalkMember().getId(), page, pageSize, 1);
        return CommonResult.success(walkTrendsDownloadNumCommonPage);
    }

    @ResponseBody
    @RequestMapping(value = {"/member/trends/getVerifyTrendsDownloadNumListToUse"}, method = RequestMethod.GET)
    public CommonResult<CommonPage<WalkTrendsDownloadNum>> getVerifyTrendsDownloadNumListToUse(Integer type, Integer goodsId,
                                                                                               @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                                               @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) throws ServiceException {

        CommonPage<WalkTrendsDownloadNum> walkTrendsDownloadNumCommonPage = walkTrendsDownloadNumService.listDownloadNumToUse(goodsId, type, this.getCurrentWalkMember().getId(), page, pageSize);
        return CommonResult.success(walkTrendsDownloadNumCommonPage);
    }

    @ResponseBody
    @RequestMapping(value = {"/member/trends/delDownloadNumByMember"}, method = RequestMethod.GET)
    public CommonResult<WalkTrendsDownloadNum> delDownloadNumByMember(Integer id) throws ServiceException {

        walkTrendsDownloadNumService.delDownloadNumByMember(id);
        return CommonResult.success(null);
    }

    @OperationLog
    @ApiOperation(value = "所有可下载的素材")
    @ResponseBody
    @RequestMapping(value = {"/member/trends/getAllMaterialNum"}, method = RequestMethod.GET)
    public CommonResult<Map> getAllMaterialNum(Integer shopId, Integer goodsId) throws ServiceException {
        Integer shopIdFor = null;
        if (shopId != null) {
            shopIdFor = shopId;
        } else {
            shopIdFor = this.getCurrentMember().getShopId();
        }
        Integer allMaterialNum = walkTrendsService.getAllMaterialNum(1, shopIdFor, goodsId);
        Integer allMaterialNum1 = walkTrendsService.getAllMaterialNum(3, shopIdFor, goodsId);
        Map map = new HashMap();
        map.put("videoNum", allMaterialNum);
        map.put("imgNum", allMaterialNum1);
        return CommonResult.success(map);
    }


}
